package com.example.demo.thread.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 测试 AtomicInteger 的原子性
 *
 * @author luowj12
 * @since 2021/4/16 15:21
 */
public class AtomicIntegerThread implements  Runnable {

    private static AtomicInteger t =new AtomicInteger(50);

    @Override
    public void run() {
        while (t.get() > 0){
            System.out.println(Thread.currentThread().getName()+": 还有"+t.get()+"张");
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":  抢到第"+t.decrementAndGet()+"张");
        }
    }
}
